/**
 * @author Kelvin Qi
 * 与sns数据交换的函数
 */
var friends = Array();
var PeoPleRequestFieldsMax = 25;
var friendsIndex = 0;
var name_owner;
var id_owner;
var url_owner;
function getData(){
	var req = opensocial.newDataRequest();
	var params = {};
	params[opensocial.DataRequest.PeopleRequestFields.MAX] = PeoPleRequestFieldsMax;
	params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.STATUS, opensocial.Person.Field.PROFILE_URL, opensocial.Person.Field.NAME, opensocial.Person.Field.THUMBNAIL_URL];
	//opensocial.Person.Field.ABOUT_ME,
	req.add(req.newFetchPersonRequest(opensocial.DataRequest.PersonId.OWNER, params), 'viewer');
	req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS', params), 'viewerFriends');
	req.send(onLoadFriends);
};

function onLoadFriends(data){
    var html = '';
    var viewer = data.get('viewer').getData();
    var uid = viewer.getField(opensocial.Person.Field.ID);
    id_owner = uid;
    var img = viewer.getField(opensocial.Person.Field.THUMBNAIL_URL);
    url_owner = img;
    var name = viewer.getDisplayName();
    var viewerFriends = data.get('viewerFriends').getData();
    name_owner = name;
    viewerFriends.each(function(person){
        friends[friendsIndex] = person;
        html += '<img src="' + person.getField(opensocial.Person.Field.THUMBNAIL_URL) + '"/>';
        friendsIndex++;
    });
    document.getElementById('downLoadImg').innerHTML = html;
    CreateMap();
};

function makePOSTRequest() {    
	 var params = {};      
	 var postdata = {       
		 id : id_owner,  
		 score : scoreNum,  
		 snsFlag : "x"    
	 };
	 params[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;	//default method is GET
	 params[gadgets.io.RequestParameters.POST_DATA] = gadgets.io.encodeValues(postdata);    
	 var url = 'http://greedy-snake.appspot.com/';     
	 gadgets.io.makeRequest(url, response, params);  
 };   
 
function response(obj) {
	var jsondata = obj.data;
	var getArr = jsondata.split("@#@");
	var rankInfoInnerHTML = '';
	var paramTitle = '<a href=http://xiaonei.com/profile.do?id=' + id_owner + '>' + name_owner + '</a>';
        paramTitle += '刚刚在“<a href="' + appURL + '">好友贪吃蛇</a>”中玩了一把！获得了'+ scoreNum +'分，快来<a href="' + appURL + '">挑战</a>一下Ta吧';
	var paramBody = '';
	if( parseInt(getArr[0]) ) {
		var allPlayer = parseInt(getArr[1]);
		var rankOfMe = parseInt(getArr[2]);
		rankInfoInnerHTML = '共有<span style="color:red;">';
		if(allPlayer == 1000) {
			rankInfoInnerHTML += '超过';
		}
		rankInfoInnerHTML += allPlayer; 
		rankInfoInnerHTML += '</span>位玩家提交了分数，本次全局排名<span style="color:red;">';
		if(allPlayer == 1000) {
			rankInfoInnerHTML += '超出';
		} else {
			rankInfoInnerHTML += '第';
		}
		rankInfoInnerHTML += rankOfMe;
		paramBody = rankInfoInnerHTML;
		paramBody += '位</span>(<a href="http://greedy-snake.appspot.com/top10" target="_blank">Top10</a>)';
		rankInfoInnerHTML += '位</span>(<a href="/top10" target="_blank">Top10</a>)';	
		//<span onclick="pop_top10();" onMouseOut="chgCursorDefault();" onMouseOver="chgCursorHand();" style="text-decoration:underline;">(Top10)</span>
	}
	paramBody += '<br/><a href="http://greedy-snake.appspot.com/top10" target="_blank">"好友贪吃蛇"战绩十大排行榜(Top10)</a>';
	document.getElementById("rankInformation").innerHTML = rankInfoInnerHTML;
	document.getElementById("restart").innerHTML = '&nbsp;&nbsp;&nbsp;<span onclick="restart();" onMouseOut="chgCursorDefault();" onMouseOver="chgCursorHand();" style="text-decoration:underline;">再来挑战一次！</span>';
	PostActivity(paramTitle, paramBody);
};

var appURL = "http://apps.xiaonei.com/greedysnake";
function PostActivity(actTitle, actBody){
    var params = {};
    params[opensocial.Activity.Field.TITLE] = actTitle;
    params[opensocial.Activity.Field.BODY] = actBody;
    var activity = opensocial.newActivity(params);
    opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH);
};